//
//  LHSingerHomeViewController.m
//  Enjoy_Music
//
//  Created by Bruce_Lin on 15/6/8.
//  Copyright (c) 2015年 Bruce. All rights reserved.
//

#import "LHSingerHomeViewController.h"
#import <UIImageView+WebCache.h>
#import <MBProgressHUD.h>
#import <MJRefresh.h>
#import "MyNavgationView.h"
#import "LHSingerHomeTableViewCell.h"
#import "LHSingerHomeModel.h"
#import "LHSingerListViewController.h"


#define KSingerHomeTableViewCell @"singerReuse"
@interface LHSingerHomeViewController ()<UITableViewDelegate, UITableViewDataSource, MBProgressHUDDelegate>
@property (nonatomic ,strong)UITableView *singerTableView;
@property (nonatomic ,strong)NSMutableArray *mainArr;
@property (nonatomic ,strong)MBProgressHUD *hud;
@property (nonatomic ,strong)MyNavgationView *navi;
@end

@implementation LHSingerHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置状态栏颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    //创建导航栏
    [self creatNavigation];
    
    self.mainArr = [NSMutableArray array];
    
    [self.view addSubview:self.singerTableView];
    
    //创建小菊花
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.hud];
    self.hud.mode = MBProgressHUDModeAnnularDeterminate;
    self.hud.delegate = self;
    self.hud.labelText = @"Loading";
    [self.hud showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
}

- (void)creatNavigation
{
    //隐藏系统导航栏
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navi = [[MyNavgationView alloc]initWithFrame:CGRectMake(0, 0, LHWidth, 64)];
    [self.view addSubview:_navi];
    [_navi creatTitle:@"歌手列表"];
    
    //更新状态栏颜色
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)getdata
{
    [LHHttpHandler get:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.2&active=1&net=2&openudid=7a702b310d3c0910aca4228a673a841751162b63&idfa=E1922CE5-ECA3-494B-9B6A-B637D35AFDC2&utdid=VXEJsNE34hwDAGokQcEmljVB&alf=201200&bundle_id=com.ttpod.music" body:nil result:LHHttp success:^(id result) {
        
#ifdef IOS_DEBUG
        NSLog(@"%@",result);
#endif
        
        for (NSDictionary *tempDic in [result objectForKey:@"data"]) {
            
            LHSingerHomeModel *model = [[LHSingerHomeModel alloc]initWithDic:tempDic];
            [_mainArr addObject:model];
        }
        
        //请求数据后刷新视图
        [_singerTableView reloadData];
        
    } failure:^(NSError *error) {
        
#ifdef IOS_DEBUG
        NSLog(@"请求失败");
#endif
    }];
}

/**
 *  singerTableView懒加载
 *
 *  @return singerTableView
 */
- (UITableView *)singerTableView
{
    if (!_singerTableView) {
        self.singerTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, LHWidth, LHHeight - 64 - 49) style:UITableViewStylePlain];
        _singerTableView.dataSource = self;
        _singerTableView.delegate = self;
        _singerTableView.backgroundColor = [UIColor blackColor];
        [_singerTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_singerTableView registerClass:[LHSingerHomeTableViewCell class] forCellReuseIdentifier:KSingerHomeTableViewCell];
    }
    return _singerTableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mainArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHSingerHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KSingerHomeTableViewCell];
    cell.titleLabel.text = [_mainArr[indexPath.row]title];
    cell.detailsLabel.text = [_mainArr[indexPath.row]details];
    
    //设置图片淡入动画
    [cell.pic sd_setImageWithURL:[NSURL URLWithString:[_mainArr[indexPath.row] pic_url]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        cell.pic.alpha = 0.0;
        [UIView transitionWithView:cell.pic duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            [cell.pic setImage:image];
            cell.pic.alpha = 1.0;
        } completion:NULL];
    }];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return LHHeight / 3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
#ifdef IOS_DEBUG
    NSLog(@"%ld",indexPath.row);
#endif
    //取消CELL点击阴影
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LHSingerListViewController *vc = [[LHSingerListViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.listId = [_mainArr[indexPath.row] listID];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [hud removeFromSuperview];
    hud = nil;
}

-(void) myProgressTask
{
    float progress = 0.0f;
    
    [self getdata];
    
    while (progress < 1.0f) {
        progress += 0.01f;
        _hud.progress = progress;
        usleep(10000);
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
